package com.financing.wap.util;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;

/**
 * 随机数工具类
 * @author Mars
 * @createDate 2014年10月5日
 */
public class RandomUtils {
	static Logger logger = Logger.getLogger(RandomUtils.class);
	/**
	 * 今天平台返佣基数
	 */
	private static BigDecimal todayAllRebatesBaseMun=null;
	/**
	 * 今天平台返佣基数key（日期字符）
	 */
	private static String todayAllRebatesBaseMunKey = null;
	
	public static Random rd=new Random();
	
	/**
	 * 获取随机数字字符串
	 * @param count 随机位数
	 * @return
	 */
	public static String getRandomUtils(int count){
		String s= RandomStringUtils.randomNumeric(count);
		return s;
	}
	/**
	 * 随机数字
	 * @param start 开始数字
	 * @param end 结束数字(不包含)
	 * @return
	 */
	public static int getRandomNumUtils(int start,int end){
		return (new Random()).nextInt(end-start)+start;
	}


	/**
	 * 平台今日收益基数
	 * @param count
	 * @return
	 */
	public static BigDecimal getTodayAllRebatesBaseMun(int count){
		String  datePattern= "yyyyMMdd";
		//		char[] c = {'0','1','2','3','4','5','6','7','8','9'};
		//		String s = RandomStringUtils.random(5, 0, 9, false, true, c);
		
		Calendar cl = Calendar.getInstance();
		cl.setTime(DateUtil.format(DateUtil.getDate(datePattern), datePattern));
		
		if(todayAllRebatesBaseMun==null){
			logger.info("--->初始化平台今日佣金基数："+todayAllRebatesBaseMunKey+":"+todayAllRebatesBaseMun);
			todayAllRebatesBaseMun = new BigDecimal(RandomStringUtils.randomNumeric(count));
			todayAllRebatesBaseMunKey=DateUtil.formatDate(cl.getTime(), datePattern);
		}else{
			logger.info("匹配今日返佣基数");
			Calendar c = Calendar.getInstance();
			c.setTime(DateUtil.format(todayAllRebatesBaseMunKey, datePattern));
			if(cl.after(c)){
				todayAllRebatesBaseMun = new BigDecimal(RandomStringUtils.randomNumeric(count));
				todayAllRebatesBaseMunKey=DateUtil.formatDate(cl.getTime(), datePattern);
			}
		}
		logger.info("--->平台今日返佣基数："+todayAllRebatesBaseMunKey+":"+todayAllRebatesBaseMun);
		return todayAllRebatesBaseMun;
	}
	/**
	 * 获取随机数字
	 * @param count 随机位数
	 * @return
	 */
	public static String  getRandomNumUtilsForString(int count){
		String s= RandomStringUtils.randomNumeric(count);
		return s;
	}


	public static void main(String[] orgs){
		RandomUtils.getTodayAllRebatesBaseMun(5);
	}
}
